import pandas as pd
from jili.core.printlog import print
from jili.factor import factordata
from jili.core import load,save
from jili.report.statistics_info import statistics_info_one
from jili.tool.convert import str2datetime
from joblib import Parallel, delayed
def stat_factor(factorid,url,start,end,objs=[]):
    f = factordata(factorid, url=url)
    start = str2datetime(start)
    end = str2datetime(end)
    d1 = {}
    rst={}
    for d,v in f.iter_data(start,end):
        for obj, vv in v.items():
            if objs:
                if obj not in objs:
                    continue
            if obj not in d1.keys():
                d1[obj] = []
            if not pd.isna(vv):
                d1[obj].append(vv)
    for obj, v in d1.items():
        if v:
            d2 = pd.Series(v)
            rr = statistics_info_one(d2, isname=False, percentiles=[5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 95], mode=0)
            rst[obj]=rr
    return rst
def deal_batch_stat_factor(factorids,url,start,end,objs=[],n_job=-1):
    rst={}
    rst0 = Parallel(n_jobs=n_job)(delayed(stat_factor)(factorid,url,start,end,objs) for factorid in factorids)
    n=0
    for fid in factorids:
        rst[fid]=rst0[n]
        n=n+1
    return rst
if __name__=="__main__":
    print("start")
    rr=stat_factor(factorid="stock_daily_hsr_1_zscore",url=r"G:/factor/stock_wave1/k1d",start="20190102",end="20240731",objs=[])
    print("end")
    print(1)